/**
 * 修改密码，找回密码，发送验证码
 * 2020-05-11
 */
'use strict';
const nodemailer = require('nodemailer')
const smtpTransport = require('nodemailer-smtp-transport');
const produceRandom = require('produce-random');
const config = {
	service: 'QQ',
	auth: {
		user: '1261974655@qq.com',
		pass: 'blmincqliypwjgbi', // 在邮件官方获取授权码
		// https://mail.qq.com/cgi-bin/frame_html?sid=Il3r6d10Ng62bCd5&r=e44820ec588276c81969dd73787eaf92
	}
}

const transporter = nodemailer.createTransport(smtpTransport(config));

exports.main = async (event, context) => {
	let {
		recipient,
		type
	} = event
	/**
	 * recipient 收件人
	 * subject 发送的主题
	 * html 发送的html内容
	 */
	let checkCode = produceRandom();
	let html = `<div>
			<h3>尊敬的队友在线用户，您好！</h3>
			<p style="text-indent: 30px;">我们检测到，您正在执行${type==='modifi' ? '修改密码' : (type === 'findBack' ? '找回密码' : '关联文档管理')}操作，为进一步保护您的账户信息安全，请在操作页面输入以下验证码。</p>
			<p style="text-indent: 30px;font-weight: bold;">验证码：${checkCode}</p>
		</div>`
	let subject = type==='modifi' ? '修改密码' : (type === 'findBack' ? '找回密码' : '关联文档管理');	
	let sendMailRes = await transporter.sendMail({
		from: config.auth.user,
		to: recipient,
		subject,
		html
	})
	
	if(sendMailRes.response == '250 OK: queued as.'){
		return {
			status: 0,
			msg: "成功",
			checkCode
		}
	} else {
		return {
			status: -1,
			msg: "稍后重试"
		}
	}
	
	return {
		status: -1,
		msg: "稍后重试"
	}
}
